package com.giornoadd.timex.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.NoResultException;

import org.hibernate.HibernateException;

import com.giornoadd.timex.model.Timesheet;
import com.giornoadd.timex.util.HibernateUtil;

/**
 * Manages database operations for Timesheet table.
 * @author anil
 */
public class TimesheetManager
{
    /**
     * Returns list of all timesheet database records with matching 
     * employeeId.
     */
    @SuppressWarnings("unchecked")
	public List<Timesheet> getTimesheets(int employeeId)
    {
        List<Timesheet> timesheetList = null;

        EntityManager entityManager = HibernateUtil.getEntityManager();
        
        try
        {
        	entityManager.getTransaction().begin();
            timesheetList = entityManager.createQuery(
                    "from Timesheet" + " where employeeId = ?1").setParameter(1,
                    employeeId).getResultList();
            entityManager.getTransaction().commit();
        }
        catch (HibernateException e)
        {
        	entityManager.getTransaction().rollback();
            throw e;
        }

        return timesheetList;
    }

    /**
     * Returns Timesheet database record with matching employeeId and
     * periodEndingDate.
     */
    public Timesheet getTimesheet(int employeeId, Date periodEndingDate)
    {
        Timesheet timesheet = null;

        EntityManager entityManager = HibernateUtil.getEntityManager();
        
        try
        {
        	entityManager.getTransaction().begin();
            timesheet = (Timesheet) entityManager.createQuery(
                    "from Timesheet" + " where employeeId = ?1"
                            + " and periodEndingDate = ?2").setParameter(1,
                    employeeId).setParameter(2, periodEndingDate).getSingleResult();
            entityManager.getTransaction().commit();
        }
        catch(NoResultException ex){
        	return null;
        }
        catch (HibernateException e)
        {
        	entityManager.getTransaction().rollback();
            throw e;
        }

        return timesheet;
    }

    /**
     * Saves a Timesheet object.
     */
    public Timesheet saveTimesheet(Timesheet timesheet)
    {
        EntityManager entityManager = HibernateUtil.getEntityManager();
        
        try
        {
        	entityManager.getTransaction().begin();
        	entityManager.persist(timesheet);
        	entityManager.flush();
        	entityManager.getTransaction().commit();
        }
        catch (HibernateException e)
        {
        	entityManager.getTransaction().rollback();
            throw e;
        }
        return timesheet;
    }

    /**
     * Returns list of all timesheet database records sorted by timesheetId
     */
    @SuppressWarnings("unchecked")
	public List<Timesheet> getTimesheets()
    {
        List<Timesheet> timesheetList = null;
        EntityManager entityManager = HibernateUtil.getEntityManager();
        try
        {
        	entityManager.getTransaction().begin();
            timesheetList = entityManager.createQuery(
                    "from Timesheet ORDER BY timesheetId").getResultList();
            entityManager.getTransaction().commit();
        }
        catch (HibernateException e)
        {
        	entityManager.getTransaction().rollback();
            throw e;
        }

        return timesheetList;
    }

    /**
     * Deletes Timesheet record with matching timeSheetId 
     */
    public void deleteTimesheet(int timesheetId)
    {
    	EntityManager entityManager = HibernateUtil.getEntityManager();
        try
        {
        	entityManager.getTransaction().begin();
        	entityManager.remove(entityManager.find(Timesheet.class, new Integer(
                    timesheetId)));
        	entityManager.flush();
        	entityManager.getTransaction().commit();
        }
        catch (HibernateException e)
        {
        	entityManager.getTransaction().rollback();
            throw e;
        }
    }

    /**
     * Returns a database Timesheet record with matching timesheetId
     * and demonstrates how a record can be locked using Hibernate.
     */
    public Timesheet getTimesheet(int timesheetId, boolean doLock)
    {
    	EntityManager entityManager = HibernateUtil.getEntityManager();
        Timesheet timesheet = null;
        try
        {
        	entityManager.getTransaction().begin();
            if (doLock)
                timesheet = (Timesheet) entityManager.find(Timesheet.class,
                        new Integer(timesheetId), LockModeType.WRITE);
            else
                timesheet = (Timesheet) entityManager.find(Timesheet.class,
                        new Integer(timesheetId));
            entityManager.getTransaction().commit();
        }
        catch (HibernateException e)
        {
        	entityManager.getTransaction().rollback();
            throw e;
        }

        return timesheet;
    }
}
